<template>
  <!-- 系统配置 -->
  <div class="systemConfig">
    <div class="header">
      <el-button type="success" size="small" @click="addHandler"
        >新增</el-button
      >
    </div>
    <!--表格开始  -->
    <el-table :data="systems" style="width: 100%">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="150" align="center">
      </el-table-column>
      <el-table-column label="值" align="center">
        <template slot-scope="scope">
          <img
            v-if="judgeHandler(scope.row.val)"
            style="width: 60px; height: 60px"
            :src="scope.row.val"
            alt=""
          />
          <div v-else>{{ scope.row.val }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="introduce" label="介绍" width="180">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small"
            plain
            @click="editHandler(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            plain
            @click="deleteHandler(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <!-- 表单开始 -->
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="introduce">
          <el-input v-model="ruleForm.introduce"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="name">
          <el-select v-model="value" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="值" prop="val">
          <el-input v-if="value == '文字'" v-model="systemForm.val" />
          <el-upload
            class="upload-demo"
            action="http://81.69.24.232/pet/jz_upload.php"
            multiple
            :limit="3"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="small"
          >取 消</el-button
        >
        <el-button type="primary" @click="submitHandler" size="small"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { get, post } from "../../utils/request";
export default {
  data() {
    return {
      systems: [],
      title: "新增系统配置",
      ruleForm: {},
      rules: {},
      dialogFormVisible: false,
      options: [
        {
          value: "文字",
          label: "文字",
        },
        {
          value: "图片",
          label: "图片",
        },
      ],
      value: "文字",
      systemForm: {},
    };
  },
  methods: {
    findAllSystem() {
      get("/baseConfig/findAll").then((res) => {
        console.log(res);
        if (res.status === 200) {
          this.systems = res.data;
        }
      });
    },
    // 判断是否是图片
    judgeHandler(val) {
      var bool;
      var sub = val.substring(val.lastIndexOf(".") + 1, val.length);
      if (sub === "jpg" || sub === "jpeg" || sub === "png") {
        bool = true;
      } else {
        bool = false;
      }
      return bool;
    },
    addHandler() {
    //   this.systemFrom = {};
      this.dialogFormVisible = true;
    },

    editHandler(data) {
      this.title = "修改员工信息";
      this.dialogFormVisible = true;
      // 浅拷贝/深拷贝
      this.ruleForm = data;
      if (this.judgeHandler(this.systemForm.val)) {
        this.value = "图片";
      } else {
        this.value = "文字";
      }
    },
    deleteHandler() {
      this.$confirm("删除信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await get("/baseConfig/deleteById", { id });
          this.queryInfo();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitHandler() {
      post("/baseConfig/saveOrUpdate").then((res) => {
        if (res.status === 200) {
          this.findAllSystem();

          this.$message({
            type: "success",
            message: res.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      });
    },
  },
  created() {
    this.findAllSystem();
  },
};
</script>

<style scoped>
</style>